package com.jiangxiaofei.citycloud.adapter;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.freshfruits.FreshFruitsGoodDetailActivity;
import com.jiangxiaofei.citycloud.bean.FreshFruitHomeBean;
import com.jiangxiaofei.citycloud.constants.StringConstants;

import java.util.List;

/**
 * @author: JiangXFei
 * @date: 2017/11/21 0021
 * @content:
 */

public class FreshHomeAdapter extends BaseQuickAdapter<FreshFruitHomeBean, BaseViewHolder> {
    public FreshHomeAdapter(int layoutResId, @Nullable List<FreshFruitHomeBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FreshFruitHomeBean item) {
        ImageView imgTitle = helper.getView(R.id.img_item_top);
        Glide.with(mContext).load(item.getImg()).into(imgTitle);
        RecyclerView recyGood = helper.getView(R.id.recy_item_good);
        GridLayoutManager layoutManager = new GridLayoutManager(mContext, 3);
        FreshHomeGoodsAdapter adapter = new FreshHomeGoodsAdapter(R.layout.item_flea_good, item.getNews());
        recyGood.setLayoutManager(layoutManager);
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Integer goodId = Integer.valueOf(item.getNews().get(position).getId());
                Intent intent=new Intent(mContext, FreshFruitsGoodDetailActivity.class);
                intent.putExtra(StringConstants.FRESHGOODID,goodId);
                mContext.startActivity(intent);
            }
        });
        recyGood.setAdapter(adapter);
    }

    public class FreshHomeGoodsAdapter extends BaseQuickAdapter<FreshFruitHomeBean.NewsBean, BaseViewHolder> {

        public FreshHomeGoodsAdapter(int layoutResId, @Nullable List<FreshFruitHomeBean.NewsBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, FreshFruitHomeBean.NewsBean item) {
            helper.setText(R.id.tv_title, item.getTitle());
            ImageView imgGood = helper.getView(R.id.img_good);
            String[] split = item.getImg().split(",");
            Glide.with(mContext).load(split[0]).into(imgGood);
            helper.setText(R.id.tv_price, item.getPrice());
            helper.setText(R.id.tv_addtime, item.getIssue());
        }
    }
}
